/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.caracol.db.AccesoDatos;
import com.caracol.db.CaracolQueryGenerator;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Roque Rueda
 */
public class Cursos {
    
    private int id;
    private int horas;
    private String aprobacion;
    private Date fecha;
    private String nombreCurso;
    private int idOficial;
    
    private static final String ID = "id";
    private static final String HORAS = "horas";
    private static final String APROBACION = "aprobacion";
    private static final String FECHA = "fecha";
    private static final String NOMBRE_CURSO ="nombre_curso";
    private static final String ID_OFICIAL = "id_oficial";
    private static final String TABLE = "cursos";
    
    private static CaracolQueryGenerator caracolQuery = new CaracolQueryGenerator();

    private Cursos(int id, int horas, String aprobacion, Date fecha, String nombreCurso, int idOficial) {
        this.id = id;
        this.horas = horas;
        this.aprobacion = aprobacion;
        this.fecha = fecha;
        this.nombreCurso = nombreCurso;
        this.idOficial = idOficial;
    }

    public Cursos() {
        this(0,0, "", new Date(), "", 0);
    }
    
    public Cursos(int horas, String aprobacion, Date fecha, String nombreCurso, int idOficial)
    {
        this(0, horas, aprobacion, fecha, nombreCurso, idOficial);
    }
    
    
    /**
     * Get a single cargo store in the database by searching it's id
     * in the table
     * @param id identifier that is going to be search
     * @return cargo instance initialize with the value of the database
     * if it's found the given id otherwise returns null
     * @throws SQLException if some db exception happend during this operation
     * this is throw again
     * @throws Exception if some exception happend during the execution of this
     * operation this is throw
     */
    public static Cursos getCurso(int id) throws SQLException, Exception {
        Cursos c = null;
        String[] columns = new String[] { ID, HORAS, APROBACION, FECHA, NOMBRE_CURSO, ID_OFICIAL };
        
        Map<String, String> where = new LinkedHashMap<String, String>();
        where.put(ID, new Integer(id).toString());
        
        List<Object> rawCargo = caracolQuery.query(columns, TABLE, where, null, null, null, null);
        
        if (rawCargo != null && rawCargo.size() > 0) {
            List<Object> row = (List<Object>) rawCargo.get(0);
            c  = new Cursos();
            c.id = id;
            c.setHoras(Integer.parseInt(row.get(1).toString()));
            c.setAprobacion(row.get(2).toString());
            c.setFecha((Date) row.get(3));
            c.setNombreCurso(row.get(4).toString());
            c.setIdOficial(Integer.parseInt(row.get(5).toString()));
        }
        
        return c;
    }
    
    /**
     * Get all the cargos that exist in the database
     * @return {}
     * @throws SQLException
     * @throws Exception 
     */
    public static List<Cursos> getAll() throws SQLException, Exception {
        ArrayList<Cursos> cursosList = new ArrayList<Cursos>();
        String[] columns = new String[] { ID,  HORAS, APROBACION, FECHA, NOMBRE_CURSO, ID_OFICIAL };
        List<Object> rawCargo = caracolQuery.query(columns, TABLE, null, null, null, null, null);
        
        for (Object object : rawCargo) {
            List<Object> row = (List<Object>) object;
            Cursos c = new Cursos();
            c.id = Integer.parseInt(row.get(0).toString());
            c.setHoras(Integer.parseInt(row.get(1).toString()));
            c.setAprobacion(row.get(2).toString());
            c.setFecha((Date) row.get(3));
            c.setNombreCurso(row.get(4).toString());
            c.setIdOficial(Integer.parseInt(row.get(5).toString()));
            cursosList.add(c);
        }
        
        return cursosList;
    }
    
    /** 
     * Saves the values of this instance in the database
     * @return true if the operation affects more than 0 rows
     * @throws SQLException if some exception related with the database happend during the excetion 
     * of this operation
     * @throws Exception if some exception happend during the operation
     */
    public boolean save() throws SQLException, Exception {
        if (getId() > 0) { // this is already save
            return true;
        }
        Map<String,Object> m = new LinkedHashMap<String,Object>();
        m.put(HORAS, getHoras());
        m.put(APROBACION, getAprobacion());
        m.put(FECHA, getFecha());
        m.put(NOMBRE_CURSO, getNombreCurso());
        m.put(ID_OFICIAL, getIdOficial());
        
        int rowsAffected = caracolQuery.insert(TABLE, m);
        
        ArrayList<Object> data = null;
        if (rowsAffected > 0) {
            AccesoDatos ad = caracolQuery.getOpenConnection();
            data = new ArrayList<Object>(ad.rawQuery("SELECT currval(\'sqcursos\');"));
        }
        if (data != null && data.size() > 0) {
            ArrayList<Object> row = (ArrayList<Object>) data.get(0);
            Double temp = Double.parseDouble(row.get(0).toString());
            this.id = temp.intValue();
            return true;
        }
        return false;
    }

    /**
     * Updates the database row with this instance values
     * @return true if rows affected is > 0
     * @throws SQLException is some db exception happen this is throw
     * @throws Exception is some exception happen this is throw
     */
    public boolean update() throws SQLException, Exception {
        int rowsAffected;
        Map<String, Object> values = new LinkedHashMap<String, Object>();
        values.put(HORAS, getHoras());
        values.put(APROBACION, getAprobacion());
        values.put(FECHA, getFecha());
        values.put(NOMBRE_CURSO, getNombreCurso());
        values.put(ID_OFICIAL, getIdOficial());
        
        
        Map<String,Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        
        rowsAffected = caracolQuery.update(TABLE, values, conditions);
        
        return rowsAffected > 0;
    }
    
    /** 
     * Delete the cargo from database this delete is made by searching in the database if this instance id exist
     * 
     * @return true if the operation affect more thant one row otherwise returns false
     * @throws SQLException if the database throws an exception during the execution of this operation
     * @throws Exception if something other than the database throws an exception this is throw
     * 
     */
    public boolean delete() throws SQLException, Exception {
        if (getId() <= 0) {
            return true;
        }

        Map<String, Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        int rowsAffected = caracolQuery.delete(TABLE, conditions);

        if (rowsAffected > 0) {
            id = 0;
        }

        return rowsAffected > 0;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the horas
     */
    public int getHoras() {
        return horas;
    }

    /**
     * @param horas the horas to set
     */
    public void setHoras(int horas) {
        this.horas = horas;
    }

    /**
     * @return the aprobacion
     */
    public String getAprobacion() {
        return aprobacion;
    }

    /**
     * @param aprobacion the aprobacion to set
     */
    public void setAprobacion(String aprobacion) {
        this.aprobacion = aprobacion;
    }

    /**
     * @return the fecha
     */
    public Date getFecha() {
        return fecha;
    }

    /**
     * @param fecha the fecha to set
     */
    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

    /**
     * @return the nombreCurso
     */
    public String getNombreCurso() {
        return nombreCurso;
    }

    /**
     * @param nombreCurso the nombreCurso to set
     */
    public void setNombreCurso(String nombreCurso) {
        this.nombreCurso = nombreCurso;
    }

    /**
     * @return the idOficial
     */
    public int getIdOficial() {
        return idOficial;
    }

    /**
     * @param idOficial the idOficial to set
     */
    public void setIdOficial(int idOficial) {
        this.idOficial = idOficial;
    }
    
}
